package com.ocm.burnningstar.Adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ocm.burnningstar.Model.UserOver;
import com.ocm.burnningstar.R;
import com.ocm.burnningstar.Utils.Helper;

import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.ocm.burnningstar.GlobalApplication.typeFaceNum;
import static com.ocm.burnningstar.GlobalApplication.typeFaceTxt;
import static com.ocm.burnningstar.R.id.header;

/**
 * Created by ocm on 2018-01-29.
 */

public class VerUserOverAdapter extends RecyclerView.Adapter<VerUserOverAdapter.ViewHolder> {
    List<UserOver> users = new ArrayList<>();
    private LayoutInflater mInflater;

    Context mContext;

    public VerUserOverAdapter(Context context, List<UserOver> list){
        mInflater = LayoutInflater.from(context);
        users = list;
        mContext = context;
    }
    private void startAnimator(final TextView view,int max) {
         final ValueAnimator animator = ValueAnimator.ofInt(0, max);
         animator.setDuration(2000);
         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
             @Override
             public void onAnimationUpdate(ValueAnimator animation) {
                 Integer value = (Integer) animation.getAnimatedValue();
                 view.setText(String.valueOf(value));
             }
         });
         animator.start();
    }
    @Override
    public VerUserOverAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.ver_item,
                parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        int screenWidth = ScreenUtils.getScreenWidth();
        int screenHeight = (int)(ScreenUtils.getScreenHeight()*1.2);
        int viewWidth = (int)(screenWidth*0.15);
        int viewHeight = (int)(screenHeight*0.56);
//        AbsListView.LayoutParams params = new AbsListView.LayoutParams(viewWidth,viewHeight);
//        view.setLayoutParams(params);

        viewHolder.header = (CircleImageView) view.findViewById(header);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.rateavg = (TextView) view.findViewById(R.id.rateavg);
        viewHolder.calorie = (TextView) view.findViewById(R.id.calorie);

        viewHolder.name.setTypeface(typeFaceTxt);
        viewHolder.rateavg.setTypeface(typeFaceNum);
        viewHolder.calorie.setTypeface(typeFaceNum);

        ViewGroup.LayoutParams hs = viewHolder.header.getLayoutParams();
        hs.width =hs.height = (int)(viewWidth*0.5);
        viewHolder.header.setLayoutParams(hs);
//        ViewGroup.LayoutParams hs = viewHolder.headerbox.getLayoutParams();
//        hs.width =hs.height = (int)(viewWidth*0.5);
//        viewHolder.header.setLayoutParams(hs);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final VerUserOverAdapter.ViewHolder holder, int position) {
        Log.i("onBindViewHolder","onBindViewHolder");
        final UserOver user = users.get(position);
//        holder.header.setImageBitmap(user.getHeader());
        holder.name.setText(user.getName());
        startAnimator(holder.rateavg,user.getHeartAvg());
        startAnimator(holder.calorie,(int)user.getCalorie());
//        holder.rateavg.setText(String.valueOf(user.getHeartAvg()));
//        holder.calorie.setText(String.valueOf(user.getCalorie()));
        try{
            ImageOptions imageOptions= new ImageOptions.Builder().setFadeIn(true).build();
            x.image().loadDrawable(user.getHeadrUrl(), imageOptions, new Callback.CacheCallback<Drawable>() {
                @Override
                public boolean onCache(Drawable result) {
                    holder.header.setImageBitmap(Helper.drawableToBitmap(result));
                    return true;
                }

                @Override
                public void onSuccess(Drawable result) {
                    Log.i("onBindViewHolder","onSuccess");
                    holder.header.setImageBitmap(Helper.drawableToBitmap(result));
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Log.i("onBindViewHolder","onError");
                    ex.printStackTrace();
                }
                @Override
                public void onCancelled(CancelledException cex) {
                    Log.i("onBindViewHolder","onCancelled");
                    cex.printStackTrace();
                }
                @Override
                public void onFinished() {

                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return users.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }
        CircleImageView header;
        TextView name;
        TextView rateavg ;
        TextView calorie;
    }

}
